package com.liulu.tools

import android.content.Context
import android.content.res.Resources
import android.util.TypedValue
import kotlin.math.roundToInt

private val displayMetrics = Resources.getSystem().displayMetrics

val Float.dp2px: Float
    @JvmName("dp2px")
    get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, displayMetrics)

val Int.dp2px: Int
    @JvmName("dp2px")
    get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), displayMetrics)
        .roundToInt()

val Float.sp2px: Float
    @JvmName("sp2px")
    get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this, displayMetrics)

val Int.sp2px: Int
    @JvmName("sp2px")
    get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.toFloat(), displayMetrics)
        .roundToInt()

val Float.px2dp: Float
    @JvmName("px2dp")
    get() = (this / displayMetrics.density)

val Int.px2dp: Int
    @JvmName("px2dp")
    get() = (this.toFloat() / displayMetrics.density).roundToInt()

val Float.px2sp: Float
    @JvmName("px2sp")
    get() = (this / displayMetrics.scaledDensity)

val Int.px2sp: Int
    @JvmName("px2sp")
    get() = (this.toFloat() / displayMetrics.scaledDensity).roundToInt()

val SCREEN_WIDTH: Int
    @JvmName("SCREEN_WIDTH")
    get() = displayMetrics.widthPixels

val SCREEN_HEIGHT: Int
    @JvmName("SCREEN_HEIGHT")
    get() = displayMetrics.heightPixels

val STATUS_BAR_HEIGHT: Int
    @JvmName("STATUS_BAR_HEIGHT")
    get() {
        val resourceId =
            Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android")
        return Resources.getSystem().getDimensionPixelSize(resourceId)
    }

val Context.ACTION_BAR_HEIGHT: Int
    @JvmName("ACTION_BAR_HEIGHT")
    get() {
        val tv = TypedValue()
        return if (theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
        } else 0
    }
val NAVIGATION_BAR_HEIGHT: Int
    @JvmName("NAVIGATION_BAR_HEIGHT")
    get() {
        val resourceId =
            Resources.getSystem().getIdentifier("navigation_bar_height", "dimen", "android")
        return if (resourceId != 0) {
            Resources.getSystem().getDimensionPixelOffset(resourceId)
        } else {
            0
        }
    }
